<?php

class Admin extends Scaff {

    public function __construct() {
        parent::__construct('administratori');
        
        if (isset($_GET['add']))
        {
            if (isset($_POST['submit']))
            {
                $form = new Form($_POST);
                if (
                $form->minlength('titlu', 15, 'Titlu') &&
                $form->minlength('text_scurt', 20, 'Text scurt') &&
                $form->minlength('text', 100, 'Text complet')
                ) 
                {
                    $_POST['data'] = 'NOW()';
                    if ($this->add($_POST)) redirect('stiri.php?all');
                }            
            }
            
            require(view('stiri_add'));
        }
        elseif (isset($_GET['view']))
        {
            $object = $this->get('id',$_GET['view']);
            require(view('stiri_view'));
        }
        elseif (isset($_GET['edit']))
        {
            if (isset($_POST['submit']))
            {
                $form = new Form($_POST);
                if (
                $form->minlength('titlu', 15, 'Titlu') &&
                $form->minlength('text_scurt', 20, 'Text scurt') &&
                $form->minlength('text', 100, 'Text complet')
                ) 
                {
                    if ($this->update($_POST, 'id', $_GET['edit'])) redirect('stiri.php?all');
                }            
            }
            
            $object = $this->get('id',$_GET['edit']);
            require(view('stiri_edit'));
        }
        elseif (isset($_GET['del']))
        {
            $this->del('id', $_GET['del']);   
        }
        elseif (isset($_GET['all']))
        {
            $objects = $this->getAll('data','DESC');
            require(view('stiri_all'));
        }
        elseif (isset($_GET['logout']))
        {
            if(isset($_COOKIE['adminid']) && isset($_COOKIE['adminnume']) && isset($_COOKIE['adminip']) && isset($_COOKIE['adminsid']))
            {
                setcookie("adminid", "", time()-60*60*24*100, "/");
                setcookie("adminnume", "", time()-60*60*24*100, "/");
                setcookie("adminip", "", time()-60*60*24*100, "/");
                setcookie("adminsid", "", time()-60*60*24*100, "/");
            }
            
            session_start();
            header ("Content-control: private");
            unset($_SESSION['aid']);
            unset($_SESSION['anume']);
            unset($_SESSION['aip']);
            unset($_SESSION['asession_id']);
            
            redirect('login.php');
        }
        else
        {
            if (logged_admin()) redirect('dashboard.php');
            
            if (isset($_POST['submit']))
            {
                $form = new Form($_POST); 
                if (
                $form->checkemail('email', 'Email') &&
                $form->minlength('parola', 6, 'Parola')                
                ) 
                {
                    //if ($this->add($_POST)) redirect('stiri.php?all');
                    // luam un user cu drepturi 1 sau 2
                    $q = "SELECT * FROM `".$this->getTable()."` WHERE `email`='".$_POST['email']."' AND `parola`='".md5($_POST['parola'])."' AND (privilegii = 1 OR privilegii = 2)";
                    $r = $this->getCustomRow($q);
                    if (!$r) setsession('error','Parola gresita sau administrator inexistent');
                    else
                    {
                        if ($_POST['minte'] == 1)
                        {
                             setcookie("adminid", $r['id'], time()+60*60*24*100, "/");
                             setcookie("adminnume", $r['nume'], time()+60*60*24*100, "/");
                             setcookie("adminip", $_SERVER['REMOTE_ADDR'], time()+60*60*24*100, "/");
                             setcookie("adminsid", session_id(), time()+60*60*24*100, "/");
                        }
                        setsession('aid', $r['id']);
                        setsession('anume', $r['nume']);
                        setsession('aip', $_SERVER['REMOTE_ADDR']);
                        setsession('asession_id',session_id());
                        
                        setsession('info','Bine ai venit, '.$r['nume'].'!');
                        redirect('dashboard.php');
                    }
                }
            }
            require(view('login'));
        }
    }
    
    public function getAll($orderby = '', $direction = 'ASC') {
        return parent::getAll($orderby, $direction);
    }
    
    public function getByPage($limit, $page = 1, $orderby = '', $direction = 'ASC') {
        return parent::getByPage($limit, $page, $orderby, $direction);
    }
    
    public function add($arr) {
        return parent::add($arr);
    }
    
    public function del($field, $value) {
        if (parent::del($field, $value)) redirect('stiri.php?all');
    }
    
    public function get($field, $value) {
        return parent::get($field, $value);
    }
    
    public function update($arr, $field, $value) {
        return parent::update($arr, $field, $value);
    }
    
}


